﻿namespace com.stifel.ProjectMgmt
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web.Caching;
    using System.Web.UI.WebControls;

    using com.stifel.Extensions.DateTimeExtensions;
    using com.stifel.Extensions.StringExtensions;
    using com.stifel.ProjectMgmtDao;

    using Microsoft.SharePoint;

    public partial class GanttDashboardUserControl
    {
        #region Fields

        string _sitecollectionurl;
        string _UrlReferrer;

        #endregion Fields

        #region Properties

        string sitecollectionurl
        {
            get
            {
                if ( string.IsNullOrEmpty( _sitecollectionurl ) )
                    _sitecollectionurl = SPContext.Current.Web.Url;
                return _sitecollectionurl;
            }
        }

        string UrlReferrer
        {
            get
            {
                if ( string.IsNullOrEmpty( _UrlReferrer ) )
                    _UrlReferrer = ( string ) Page.Cache.Get( CacheKey.UrlReferrer );
                if ( string.IsNullOrEmpty( _UrlReferrer ) )
                    setUrlReferrer( );
                return _UrlReferrer;
            }
            set
            {
                _UrlReferrer = value;
                addToCache( CacheKey.UrlReferrer, value );
            }
        }

        #endregion Properties

        #region Methods

        void addToCache( string key, object value )
        {
            if ( value == null )
                Cache.Remove( key );
            else
                // cache for 10 hours
                Cache.Insert( key, value, null,
                    Cache.NoAbsoluteExpiration,
                    new TimeSpan( 10, 0, 0 ),
                    CacheItemPriority.Normal,
                    new CacheItemRemovedCallback(
                        delegate( string k, object v, CacheItemRemovedReason reason )
                        {
                        } ) );
        }

        void load( )
        {
            bool showByDueDate = uiShowByDueDate.Checked;
            bool showOnlyImpactedProjects = uiShowOnlyImpactedProjects.Checked;

            string[ ] s = uiYear.SelectedValue.Split( '\t' );
            int y = s[ 0 ].ToInt32( );
            int q = s[ 1 ].ToInt32( );

            StringBuilder sb = new StringBuilder( );
            sb.AppendFormat( "<b>Current Selection:</b> {0}", uiYear.SelectedItem.Text );
            if ( showOnlyImpactedProjects )
                sb.Append( "&nbsp;&nbsp;<b>Show only the projects with impact within quarter</b>" );
            else
                sb.Append( "&nbsp;&nbsp;<b>Show all projects</b>" );
            if ( showByDueDate )
                sb.Append( "&nbsp;<b>by due date</b>" );
            else
                sb.Append( "&nbsp;<b>by team</b>" );
            uiLblCurrentSelection.Text = sb.ToString( );
            sb = null;

            List<ProjectSite> sites = new List<ProjectSite>( );
            StifelProjectDao dao = new StifelProjectDao( );
            sites = dao.GetSitesWithProjectsDefined( sitecollectionurl );

            List<StifelProject> projects = new List<StifelProject>( );
            projects = dao.GetActiveProjects( sites );

            uiNumberOfSites.Text = string.Format( "<span class=\"miscinfo\">Projects found in {0} site{1}.</span>", sites.Count, sites.Count == 1 ? string.Empty : "s" );

            uiProjectGantt.Controls.Add( new ProjectGantt
            {
                Font = new System.Drawing.Font( "Arial", 6 ),
                Projects = projects,
                Quarter = q,
                ShowByDueDate = showByDueDate,
                ShowOnlyImpactedProjects = showOnlyImpactedProjects,
                Year = y
            } );
        }

        void loadDefaults( )
        {
            uiShowByDueDate.Checked = ShowByDueDate;
            uiShowOnlyImpactedProjects.Checked = ShowOnlyImpactedProjects;
        }

        void loadYearQuarter( )
        {
            uiYear.Items.Clear( );
            DateTime now = DateTime.Now;
            int year = now.Year;
            int quarter = now.GetQuarter( );
            bool selected = false;
            int j = 0;
            quarter--;
            if ( quarter <= 0 )
            {
                quarter = 4;
                year--;
            }
            for ( int i = quarter; i <= 4; i++ )
            {
                j++;
                uiYear.Items.Add( new ListItem
                {
                    Selected = selected,
                    Text = string.Format( "Q{1} {0}{2}", year, i, selected ? " (current)" : string.Empty ),
                    Value = string.Format( "{0}\t{1}", year, i )
                } );
                selected = j == 1;
            }
            year++;
            for ( int i = 1; i <= 4 - j; i++ )
            {
                uiYear.Items.Add( new ListItem
                {
                    Selected = selected,
                    Text = string.Format( "Q{1} {0}", year, i ),
                    Value = string.Format( "{0}\t{1}", year, i )
                } );
            }
        }

        void setUrlReferrer( )
        {
            if ( Request != null && Request.UrlReferrer != null )
                UrlReferrer = Request.UrlReferrer.ToString( );
            else if ( Request != null )
                UrlReferrer = Request.Url.ToString( );
        }

        #endregion Methods
    }
}